package com.ktjy.t382.ch01.pojo;

/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */

import java.io.Serializable;

/**
 * 响应数据封装工具
 *
 * @author 科泰集团教研团队
 * @version 1.0.0
 * @createTime 2024-4-17 20:49:54
 * @lastModify 2024-4-17 20:49:54
 */

public class R<T> implements Serializable {

    public static final int OK_CODE = 0;
    public static final String OK_MSG = "操作成功！";

    public static final int FAIL_CODE = 1;
    public static final String FAIL_MSG = "操作失败！";


    private int code;
    private String msg;
    private T data;

    public static <T> R<T> ok() {
        return ok(null);
    }

    public static <T> R<T> ok(T data) {
        return ok(OK_CODE, OK_MSG, data);
    }

    public static <T> R<T> ok(int code, String msg, T data) {
        return new R<T>().code(code)
                .msg(msg)
                .data(data);
    }

    public static <T> R<T> fail() {
        return fail(null);
    }

    public static <T> R<T> fail(T data) {
        return fail(FAIL_CODE, FAIL_MSG, data);
    }


    public static <T> R<T> fail(int code, String msg, T data) {
        return new R<T>().code(code)
                .msg(msg)
                .data(data);
    }

    public R<T> code(int code) {
        this.code = code;
        return this;
    }

    public R<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    public R<T> data(T data) {
        this.data = data;
        return this;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
